package com.zhou.bjgl.bjgl.business_web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhou.bjgl.bjgl.business_web.entity.BHoliday;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.myAnnotations.UserLoginToken;
import com.zhou.bjgl.bjgl.req.BaseModel;
import com.zhou.bjgl.bjgl.req.b_holiday.BHlidayChartsReq;
import com.zhou.bjgl.bjgl.req.b_holiday.BHolidayQueryReq;
import com.zhou.bjgl.bjgl.resp.BHolidayResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.resp.common.ApiRestResponse;
import com.zhou.bjgl.bjgl.business_web.service.BHolidaySerivce;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  请假模块
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@RestController
@RequestMapping("/bjgl/holiday")
@Api("请假管理")
public class BHolidayController extends BaseModel {

    @Resource
    private BHolidaySerivce bHolidaySerivce;

    @ApiOperation("申请请假")
    @PostMapping("/apply")
    @UserLoginToken
    public ApiRestResponse holidayApply(@RequestBody BHolidayQueryReq req) {
        bHolidaySerivce.apply(req);
        return ApiRestResponse.success();
    }

    @ApiOperation("获取请假列表")
    @GetMapping("/getHolidayList")
    @UserLoginToken
    public ApiRestResponse getholidayApplyList(BHolidayQueryReq req) {
        PageResp<BHolidayResp> bHolidayResps= bHolidaySerivce.listholidayApply(req);
        return ApiRestResponse.success(bHolidayResps);
    }

    @ApiOperation("删除请假申请")
    @PostMapping("/delHolidyApply")
    @UserLoginToken
    public ApiRestResponse delHolidyApply(@RequestBody BHolidayQueryReq req) {

        if (ObjectUtils.isEmpty(req.getId())) {
            throw new BwException(BwExceptionEnum.ID_IS_NULL);
        }
        bHolidaySerivce.removeById(req.getId());
        return ApiRestResponse.success();
    }

    @ApiOperation("是否同意请假")
    @PostMapping("/isAgree")
    @UserLoginToken
    public ApiRestResponse toAgreeApply(@RequestBody BHolidayQueryReq req) {
        BHoliday bHoliday = new BHoliday();
        bHoliday.setAgree(req.getAgree());
        bHoliday.setId(req.getId());
        //检查是否已经操作
        BHoliday holidayOld = bHolidaySerivce.getById(bHoliday.getId());
        if (holidayOld.getAgree() != null) {
            throw new BwException(BwExceptionEnum.NOT_REPEAT_OPERATION);
        }
        boolean b = bHolidaySerivce.updateById(bHoliday);
        if (b) {
            return ApiRestResponse.success();
        }
        return ApiRestResponse.error(BwExceptionEnum.UPDATE_FAILID);
    }

    @ApiOperation("已经抵达位置")
    @PostMapping("/check")
    @UserLoginToken
    public ApiRestResponse checkArrivedApply(@RequestBody BHolidayQueryReq req) {
        bHolidaySerivce.check(req);
        return ApiRestResponse.success();
    }

    @ApiOperation("返校打卡")
    @PostMapping("/checkBack")
    @UserLoginToken
    public ApiRestResponse checkBackApply(@RequestBody BHolidayQueryReq req) {
        bHolidaySerivce.checkBack(req);
        return ApiRestResponse.success();
    }

    @ApiOperation("取消请假")
    @PostMapping("/eliminateHoliday")
    @UserLoginToken
    public ApiRestResponse eliminateHoliday(@RequestBody BHolidayQueryReq req) {
        BHoliday bHoliday = new BHoliday();
        bHoliday.setId(req.getId());
        bHoliday.setChecked(5);
        bHolidaySerivce.updateById(bHoliday);
        return ApiRestResponse.success();
    }

    @ApiOperation("销毁请假")
    @PostMapping("/cancelHoliday")
    @UserLoginToken
    public ApiRestResponse cancelHoliday(@RequestBody BHolidayQueryReq req) {
        BHoliday bHoliday = new BHoliday();
        bHoliday.setId(req.getId());
        if (req.getNotArrivedReson() != null) {
            bHoliday.setNotArrivedReson(req.getNotArrivedReson());
            bHoliday.setChecked(0);
        } else if (req.getNotBackReson() != null) {
            bHoliday.setNotBackReson(req.getNotBackReson());
            bHoliday.setChecked(4);
        } else {
            throw new BwException(BwExceptionEnum.NO_MSG);
        }
        bHolidaySerivce.updateById(bHoliday);
        return ApiRestResponse.success();
    }

    @ApiOperation("获取请假图示数据")
    @GetMapping("/getHolidayChartData")
    @UserLoginToken
    public ApiRestResponse getHolidayChartData(BHlidayChartsReq bHlidayChartsReq) {
        if (!ObjectUtils.isEmpty(threadLocal.get().getClassId())) {
            bHlidayChartsReq.setClassId(threadLocal.get().getClassId());
        }

        HashMap<String, List> chartResult=bHolidaySerivce.queryHolidayChartData(bHlidayChartsReq);
        return ApiRestResponse.success(chartResult);
    }
}

